function value(age, name) {
    this.age = age;
    this.name = name;
}

function node(value) {
    this.value = value;
    this.next = null;
}


function List() {
    this.head = new node();

    if (!List.prototype.appendNode) {
        List.prototype.appendNode = function (node) {
            var head_temp = this.head;

            while (head_temp.next) {
                head_temp = head_temp.next;
            }

            head_temp.next = node;

        }

    }

    if (!List.prototype.showList) {
        List.prototype.showList = function () {
            var node = this.head.next;

            while (node) {
                console.info(node.value.name + ":" + node.value.age);
                node = node.next;
            }
        }
    }

    if (!List.prototype.lens) {
        List.prototype.lens = function () {
            var i = 0;
            var node = this.head.next;
            while (node) {
                node = node.next;
                i++;
            }
            return i;
        }
    }

    if (!List.prototype.insert) {
        List.prototype.insert = function (pos, node) {
            var i = 0;
            var p = this.head;
            while (null != p && i < pos - 1) {
                p = p.next;
                i++;
            }

            if (i > pos - 1 || null == p) {
                return false;
            }
            node.next = p.next;
            p.next = node;
            return true;
        }
    }

    if (!List.prototype.del) {
        List.prototype.del = function (pos) {
            var i = 0;
            var p = this.head;
            while (null != p.next && i < pos - 1) {
                p = p.next;
                i++;
            }

            if (i > pos - 1 || null == p.next) {
                return false;
            }

            p.next = p.next.next;
            return true;
        }
    }

    if (!List.prototype.sortByAge) {
        List.prototype.sortByAge = function () {

            var len = this.lens();
            for (var i = 0; i < len; i++) {
                var temp_head = this.head;
                for (var j = 0; j < len - 1 - i; j++) {
                    var p = temp_head.next;
                    var q = p.next;

                    if (p.value.age > q.value.age) {
                        temp_head.next = q;
                        p.next = q.next;
                        q.next = p;
                        temp_head = q;
                    }
                }
            }
        }
    }
}


var list = new List();
var v1 = new value(14, "jack");
var v2 = new value(12, "peter");
var v11 = new value(10, "jackji");
var n1 = new node(v1);
var n2 = new node(v2);
var n11 = new node(v11);
list.appendNode(n1);
list.appendNode(n2);
list.appendNode(n11);
list.showList();
console.info("len:" + list.lens())
var v3 = new value(99, 'p');
var n3 = new node(v3);
console.info("------insert-----")
list.insert(2, n3);
list.showList();

console.info("------delete the second node-----")
list.del(2);
list.showList();

console.info("-------sortbyage----------")
list.sortByAge();
list.showList();
